// SPDX-License-Identifier: MPL-2.0
// (c) Hare authors <https://harelang.org>

use bytes;


// Testcases have been generated with help of pycryptdome.

type multc = struct {
	a: []u8,
	b: []u8,
	x: []u8,
	y: []u8,
	result: u32,
	expected: []u8,
};

fn tmuladd(c: *curve, tcs: []multc) void = {
	for (let i = 0z; i < len(tcs); i += 1) {
		let t = tcs[i];
		assert(c.muladd(t.a, t.b, t.x, t.y) == t.result);
		if (t.result == 1) {
			assert(bytes::equal(t.a, t.expected));
		};
	};
};

@test fn p256_mulgen() void = {
	const c = p256;
	// multiply by 1
	let m1: [P256_SCALARSZ]u8 = [0...];
	m1[len(m1) - 1] = 1;

	let g = alloc(P256_G);
	defer free(g);
	assert(c.mul(g, m1) == 1);
	assert(bytes::equal(g, P256_G));

	assert(c.mulgen(g, m1) == 65);
	assert(bytes::equal(g, P256_G));

	// multiply by order - 1
	let o = alloc(P256_N);
	defer free(o);
	o[len(o) - 1] -= 1;

	const expected: [_]u8 = [
		0x04, 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, 0xf8,
		0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, 0x77, 0x03, 0x7d,
		0x81, 0x2d, 0xeb, 0x33, 0xa0, 0xf4, 0xa1, 0x39, 0x45, 0xd8,
		0x98, 0xc2, 0x96, 0xb0, 0x1c, 0xbd, 0x1c, 0x01, 0xe5, 0x80,
		0x65, 0x71, 0x18, 0x14, 0xb5, 0x83, 0xf0, 0x61, 0xe9, 0xd4,
		0x31, 0xcc, 0xa9, 0x94, 0xce, 0xa1, 0x31, 0x34, 0x49, 0xbf,
		0x97, 0xc8, 0x40, 0xae, 0x0a,
	];

	assert(c.mul(g, o) == 1);
	assert(bytes::equal(g, expected));

	let g = alloc(P256_G);
	defer free(g);
	assert(c.mulgen(g, o) == 65);
	assert(bytes::equal(g, expected));
	let priv: [_]u8 = [
		0xde, 0x5c, 0x88, 0x05, 0x42, 0xa0, 0x71, 0xe2, 0xf6, 0xfe,
		0xd0, 0xdc, 0x80, 0x07, 0x37, 0xc4, 0x35, 0xa6, 0x29, 0x48,
		0x85, 0x70, 0x4f, 0x54, 0x1c, 0x41, 0x89, 0xaf, 0xf6, 0xbc,
		0xb5, 0x19,
	];
	const expected: [_]u8 = [
		0x04, 0x8a, 0x0d, 0x13, 0x84, 0x8e, 0x4f, 0xdb, 0x05, 0x83,
		0x8e, 0x76, 0x24, 0xf9, 0x8a, 0xb2, 0x83, 0x5a, 0x79, 0xb0,
		0x65, 0x63, 0x9b, 0x4c, 0x30, 0xcf, 0x69, 0x31, 0x46, 0xf7,
		0x10, 0x49, 0x13, 0x46, 0x0b, 0xb0, 0x4e, 0x3e, 0x44, 0x7f,
		0xad, 0x96, 0xfa, 0xfb, 0xc5, 0x78, 0xc0, 0xee, 0xc6, 0xc4,
		0x88, 0xa3, 0xe5, 0xc3, 0x10, 0x3c, 0x63, 0x24, 0xc1, 0x48,
		0x43, 0x37, 0x13, 0xdf, 0xc9,
	];

	let g = alloc(P256_G);
	defer free(g);
	assert(c.mul(g, priv) == 1);
	assert(bytes::equal(g, expected));

	let r: [65]u8 = [0...];
	assert(c.mulgen(r, priv) == 65);
	assert(bytes::equal(r, expected));
};

@test fn p256_muladd() void = {
	let c = p256;

	let tcs: [_]multc = [
		multc {
			a = [
				0x04, 0xee, 0x19, 0x71, 0x69, 0xae, 0xbd, 0x94,
				0xc7, 0x8d, 0x4e, 0x7f, 0x05, 0x06, 0xb4, 0xdf,
				0x60, 0x43, 0x22, 0x2c, 0x50, 0x8d, 0x33, 0x8a,
				0x5a, 0xd0, 0x02, 0x60, 0x95, 0x5e, 0xda, 0x31,
				0x64, 0x4c, 0x7e, 0x63, 0xa9, 0xa0, 0xaf, 0x56,
				0xbb, 0x7c, 0x03, 0x84, 0x8a, 0x44, 0x30, 0xfd,
				0x39, 0x1d, 0xc0, 0x0f, 0x5c, 0x50, 0xcf, 0xda,
				0xf1, 0x99, 0x69, 0x8e, 0x77, 0x62, 0x56, 0x34,
				0xce,
			],
			b = [],
			x = [
				0xef, 0xcb, 0x7a, 0x72, 0x9c, 0xb4, 0x6a, 0xe6,
				0xb4, 0x66, 0xa6, 0x8c, 0x1a, 0x18, 0x64, 0x5c,
				0xd8, 0xdc, 0x9d, 0xa2, 0x5b, 0x48, 0x45, 0xcd,
				0x68, 0x0c, 0x5f, 0x93, 0x1b, 0x74, 0x42, 0x0b,
			],
			y = [
				0x7b, 0x6b, 0x8f, 0x14, 0x8b, 0x7a, 0x5d, 0xbe,
				0xa5, 0xd0, 0x6b, 0x7a, 0x3c, 0xaa, 0x6f, 0x4b,
				0x2a, 0xeb, 0x52, 0x7e, 0x80, 0xdb, 0xc5, 0x95,
				0x73, 0x91, 0xab, 0x3d, 0x29, 0xbc, 0xbd, 0x74,
			],
			result = 1,
			expected = [
				0x04, 0x85, 0x93, 0xaf, 0x32, 0x68, 0x1c, 0xa8,
				0xba, 0xf8, 0x64, 0x4e, 0xd3, 0xd8, 0xf3, 0x52,
				0x88, 0xdf, 0x3b, 0x92, 0x5b, 0xb4, 0xbb, 0xf6,
				0xd4, 0x2c, 0xce, 0x42, 0x8c, 0xa5, 0x9b, 0xd4,
				0xb0, 0x87, 0xac, 0xfe, 0x83, 0xcc, 0x11, 0xd1,
				0x98, 0x3e, 0x63, 0xc8, 0xba, 0xc1, 0x8e, 0x7e,
				0xf2, 0xd1, 0xa7, 0x78, 0x93, 0xa8, 0x71, 0x9e,
				0xee, 0xd2, 0xf9, 0xe3, 0xf3, 0x0a, 0xda, 0xf6,
				0x26,
			],
		},
		multc {
			a = [
				0x04, 0xee, 0x19, 0x71, 0x69, 0xae, 0xbd, 0x94,
				0xc7, 0x8d, 0x4e, 0x7f, 0x05, 0x06, 0xb4, 0xdf,
				0x60, 0x43, 0x22, 0x2c, 0x50, 0x8d, 0x33, 0x8a,
				0x5a, 0xd0, 0x02, 0x60, 0x95, 0x5e, 0xda, 0x31,
				0x64, 0x4c, 0x7e, 0x63, 0xa9, 0xa0, 0xaf, 0x56,
				0xbb, 0x7c, 0x03, 0x84, 0x8a, 0x44, 0x30, 0xfd,
				0x39, 0x1d, 0xc0, 0x0f, 0x5c, 0x50, 0xcf, 0xda,
				0xf1, 0x99, 0x69, 0x8e, 0x77, 0x62, 0x56, 0x34,
				0xce,
			],
			b = [
				0x04, 0xbd, 0x52, 0x07, 0x83, 0x40, 0x88, 0x6a,
				0xa5, 0x24, 0xd8, 0x22, 0x13, 0x4f, 0xc3, 0xf3,
				0x03, 0xca, 0xe1, 0xd3, 0x5e, 0x01, 0x95, 0x82,
				0x5f, 0xa9, 0x95, 0x9f, 0xc3, 0xc4, 0x92, 0x8f,
				0xd2, 0x30, 0x18, 0x56, 0x29, 0x93, 0x74, 0x50,
				0xbd, 0xa6, 0x8d, 0x88, 0xf6, 0x03, 0xd6, 0x16,
				0xd9, 0x9d, 0x01, 0x82, 0xbe, 0x08, 0x13, 0xec,
				0x9f, 0xb4, 0xb1, 0x18, 0xbc, 0x14, 0x09, 0x31,
				0xad,
			],
			x = [
				0xef, 0xcb, 0x7a, 0x72, 0x9c, 0xb4, 0x6a, 0xe6,
				0xb4, 0x66, 0xa6, 0x8c, 0x1a, 0x18, 0x64, 0x5c,
				0xd8, 0xdc, 0x9d, 0xa2, 0x5b, 0x48, 0x45, 0xcd,
				0x68, 0x0c, 0x5f, 0x93, 0x1b, 0x74, 0x42, 0x0b,
			],
			y = [
				0x7b, 0x6b, 0x8f, 0x14, 0x8b, 0x7a, 0x5d, 0xbe,
				0xa5, 0xd0, 0x6b, 0x7a, 0x3c, 0xaa, 0x6f, 0x4b,
				0x2a, 0xeb, 0x52, 0x7e, 0x80, 0xdb, 0xc5, 0x95,
				0x73, 0x91, 0xab, 0x3d, 0x29, 0xbc, 0xbd, 0x74,
			],
			result = 1,
			expected = [
				0x04, 0x7f, 0x66, 0x4b, 0x8c, 0x3e, 0x64, 0x5b,
				0xc1, 0x97, 0x60, 0xac, 0xa6, 0xc3, 0x0c, 0x17,
				0x80, 0xff, 0xd0, 0x95, 0xcd, 0x4d, 0x5b, 0xf1,
				0x35, 0x25, 0x53, 0x5a, 0xec, 0xff, 0xb1, 0xd9,
				0xdb, 0xdd, 0x96, 0xe3, 0xfb, 0x84, 0x68, 0x2b,
				0x53, 0x04, 0x4b, 0xcd, 0x7c, 0x4e, 0x10, 0xfa,
				0x87, 0x4c, 0x14, 0x43, 0xf0, 0x1b, 0x57, 0x39,
				0x02, 0x35, 0x44, 0xef, 0xa0, 0x38, 0xb5, 0x70,
				0xb8,
			],
		},
		// invalid a
		multc {
			a = [
				0x04, 0xee, 0x19, 0x71, 0x69, 0xae, 0xbd, 0x94,
				0xc7, 0x8d, 0x4e, 0x0f, 0x05, 0x06, 0xb4, 0xdf,
				0x60, 0x43, 0x22, 0x0c, 0x50, 0x8d, 0x33, 0x8a,
				0x5a, 0xd0, 0x02, 0x00, 0x95, 0x5e, 0xda, 0x31,
				0x64, 0x4c, 0x7e, 0x03, 0xa9, 0xa0, 0xaf, 0x56,
				0xbb, 0x7c, 0x03, 0x04, 0x8a, 0x44, 0x30, 0xfd,
				0x39, 0x1d, 0xc0, 0x0f, 0x5c, 0x50, 0xcf, 0xda,
				0xf1, 0x99, 0x69, 0x0e, 0x77, 0x62, 0x56, 0x34,
				0xce,
			],
			b = [
				0x04, 0xbd, 0x52, 0x07, 0x83, 0x40, 0x88, 0x6a,
				0xa5, 0x24, 0xd8, 0x22, 0x13, 0x4f, 0xc3, 0xf3,
				0x03, 0xca, 0xe1, 0xd3, 0x5e, 0x01, 0x95, 0x82,
				0x5f, 0xa9, 0x95, 0x9f, 0xc3, 0xc4, 0x92, 0x8f,
				0xd2, 0x30, 0x18, 0x56, 0x29, 0x93, 0x74, 0x50,
				0xbd, 0xa6, 0x8d, 0x88, 0xf6, 0x03, 0xd6, 0x16,
				0xd9, 0x9d, 0x01, 0x82, 0xbe, 0x08, 0x13, 0xec,
				0x9f, 0xb4, 0xb1, 0x18, 0xbc, 0x14, 0x09, 0x31,
				0xad,
			],
			x = [
				0xef, 0xcb, 0x7a, 0x72, 0x9c, 0xb4, 0x6a, 0xe6,
				0xb4, 0x66, 0xa6, 0x8c, 0x1a, 0x18, 0x64, 0x5c,
				0xd8, 0xdc, 0x9d, 0xa2, 0x5b, 0x48, 0x45, 0xcd,
				0x68, 0x0c, 0x5f, 0x93, 0x1b, 0x74, 0x42, 0x0b,
			],
			y = [
				0x7b, 0x6b, 0x8f, 0x14, 0x8b, 0x7a, 0x5d, 0xbe,
				0xa5, 0xd0, 0x6b, 0x7a, 0x3c, 0xaa, 0x6f, 0x4b,
				0x2a, 0xeb, 0x52, 0x7e, 0x80, 0xdb, 0xc5, 0x95,
				0x73, 0x91, 0xab, 0x3d, 0x29, 0xbc, 0xbd, 0x74,
			],
			result = 0,
			expected = [],
		},
		// invalid b
		multc {
			a = [
				0x04, 0xee, 0x19, 0x71, 0x69, 0xae, 0xbd, 0x94,
				0xc7, 0x8d, 0x4e, 0x7f, 0x05, 0x06, 0xb4, 0xdf,
				0x60, 0x43, 0x22, 0x2c, 0x50, 0x8d, 0x33, 0x8a,
				0x5a, 0xd0, 0x02, 0x60, 0x95, 0x5e, 0xda, 0x31,
				0x64, 0x4c, 0x7e, 0x63, 0xa9, 0xa0, 0xaf, 0x56,
				0xbb, 0x7c, 0x03, 0x84, 0x8a, 0x44, 0x30, 0xfd,
				0x39, 0x1d, 0xc0, 0x0f, 0x5c, 0x50, 0xcf, 0xda,
				0xf1, 0x99, 0x69, 0x8e, 0x77, 0x62, 0x56, 0x34,
				0xce,
			],
			b = [
				0x04, 0xbd, 0x52, 0x07, 0x83, 0x40, 0x88, 0x6a,
				0xf5, 0x24, 0xd8, 0x22, 0x13, 0x4f, 0xc3, 0xf3,
				0xf3, 0xca, 0xe1, 0xd3, 0x5e, 0x01, 0x95, 0x82,
				0x3f, 0xa9, 0x95, 0x9f, 0xc3, 0xc4, 0x92, 0x8f,
				0x52, 0x30, 0x18, 0x56, 0x29, 0x93, 0x74, 0x50,
				0x1d, 0xa6, 0x8d, 0x88, 0xf6, 0x03, 0xd6, 0x16,
				0xd9, 0x9d, 0x01, 0x82, 0xbe, 0x08, 0x13, 0xec,
				0x9f, 0xb4, 0xb1, 0x18, 0xbc, 0x14, 0x09, 0x31,
				0xad,
			],
			x = [
				0xef, 0xcb, 0x7a, 0x72, 0x9c, 0xb4, 0x6a, 0xe6,
				0xb4, 0x66, 0xa6, 0x8c, 0x1a, 0x18, 0x64, 0x5c,
				0xd8, 0xdc, 0x9d, 0xa2, 0x5b, 0x48, 0x45, 0xcd,
				0x68, 0x0c, 0x5f, 0x93, 0x1b, 0x74, 0x42, 0x0b,
			],
			y = [
				0x7b, 0x6b, 0x8f, 0x14, 0x8b, 0x7a, 0x5d, 0xbe,
				0xa5, 0xd0, 0x6b, 0x7a, 0x3c, 0xaa, 0x6f, 0x4b,
				0x2a, 0xeb, 0x52, 0x7e, 0x80, 0xdb, 0xc5, 0x95,
				0x73, 0x91, 0xab, 0x3d, 0x29, 0xbc, 0xbd, 0x74,
			],
			result = 0,
			expected = [],
		},
		// invalid a and b
		multc {
			a = [
				0x04, 0xee, 0x19, 0x71, 0x69, 0xae, 0xbd, 0x94,
				0xc7, 0x8d, 0x4e, 0x0f, 0x05, 0x06, 0xb4, 0xdf,
				0x60, 0x43, 0x22, 0x0c, 0x50, 0x8d, 0x33, 0x8a,
				0x5a, 0xd0, 0x02, 0x00, 0x95, 0x5e, 0xda, 0x31,
				0x64, 0x4c, 0x7e, 0x03, 0xa9, 0xa0, 0xaf, 0x56,
				0xbb, 0x7c, 0x03, 0x04, 0x8a, 0x44, 0x30, 0xfd,
				0x39, 0x1d, 0xc0, 0x0f, 0x5c, 0x50, 0xcf, 0xda,
				0xf1, 0x99, 0x69, 0x0e, 0x77, 0x62, 0x56, 0x34,
				0xce,
			],
			b = [
				0x04, 0xbd, 0x52, 0x07, 0x83, 0xf0, 0x88, 0x6a,
				0xa5, 0x24, 0xd8, 0x22, 0x13, 0xff, 0xc3, 0xf3,
				0x03, 0xca, 0xe1, 0xd3, 0x5e, 0xf1, 0x95, 0x82,
				0x5f, 0xa9, 0x95, 0x9f, 0xc3, 0xf4, 0x92, 0x8f,
				0xd2, 0x30, 0x18, 0x56, 0x29, 0xf3, 0x74, 0x50,
				0xbd, 0xa6, 0x8d, 0x88, 0xf6, 0xf3, 0xd6, 0x16,
				0xd9, 0x9d, 0x01, 0x82, 0xbe, 0xf8, 0x13, 0xec,
				0x9f, 0xb4, 0xb1, 0x18, 0xbc, 0x14, 0x09, 0x31,
				0xad,
			],
			x = [
				0xef, 0xcb, 0x7a, 0x72, 0x9c, 0xb4, 0x6a, 0xe6,
				0xb4, 0x66, 0xa6, 0x8c, 0x1a, 0x18, 0x64, 0x5c,
				0xd8, 0xdc, 0x9d, 0xa2, 0x5b, 0x48, 0x45, 0xcd,
				0x68, 0x0c, 0x5f, 0x93, 0x1b, 0x74, 0x42, 0x0b,
			],
			y = [
				0x7b, 0x6b, 0x8f, 0x14, 0x8b, 0x7a, 0x5d, 0xbe,
				0xa5, 0xd0, 0x6b, 0x7a, 0x3c, 0xaa, 0x6f, 0x4b,
				0x2a, 0xeb, 0x52, 0x7e, 0x80, 0xdb, 0xc5, 0x95,
				0x73, 0x91, 0xab, 0x3d, 0x29, 0xbc, 0xbd, 0x74,
			],
			result = 0,
			expected = [],
		}
	];

	tmuladd(p256, tcs);
};

@test fn p384_mulgen() void = {
	let c = p384;

	// multiply by 1
	let m1: [P384_SCALARSZ]u8 = [0...];
	m1[len(m1) - 1] = 1;

	let g = alloc(P384_G);
	defer free(g);
	assert(c.mul(g, m1) == 1);
	assert(bytes::equal(g, P384_G));

	assert(c.mulgen(g, m1) == 97);
	assert(bytes::equal(g, P384_G));

	// multiply by order - 1
	let o = alloc(P384_N);
	defer free(o);
	o[len(o) - 1] -= 1;

	const expected: [_]u8 = [
		0x04, 0xaa, 0x87, 0xca, 0x22, 0xbe, 0x8b, 0x05, 0x37, 0x8e,
		0xb1, 0xc7, 0x1e, 0xf3, 0x20, 0xad, 0x74, 0x6e, 0x1d, 0x3b,
		0x62, 0x8b, 0xa7, 0x9b, 0x98, 0x59, 0xf7, 0x41, 0xe0, 0x82,
		0x54, 0x2a, 0x38, 0x55, 0x02, 0xf2, 0x5d, 0xbf, 0x55, 0x29,
		0x6c, 0x3a, 0x54, 0x5e, 0x38, 0x72, 0x76, 0x0a, 0xb7, 0xc9,
		0xe8, 0x21, 0xb5, 0x69, 0xd9, 0xd3, 0x90, 0xa2, 0x61, 0x67,
		0x40, 0x6d, 0x6d, 0x23, 0xd6, 0x07, 0x0b, 0xe2, 0x42, 0xd7,
		0x65, 0xeb, 0x83, 0x16, 0x25, 0xce, 0xec, 0x4a, 0x0f, 0x47,
		0x3e, 0xf5, 0x9f, 0x4e, 0x30, 0xe2, 0x81, 0x7e, 0x62, 0x85,
		0xbc, 0xe2, 0x84, 0x6f, 0x15, 0xf1, 0xa0,
	];

	assert(c.mul(g, o) == 1);
	assert(bytes::equal(g, expected));

	let g = alloc(P384_G);
	defer free(g);
	assert(c.mulgen(g, o) == 97);
	assert(bytes::equal(g, expected));

	// random example
	let priv: [_]u8 = [
		0xde, 0x5c, 0x88, 0x05, 0x42, 0xa0, 0x71, 0xe2, 0xf6, 0xfe,
		0xd0, 0xdc, 0x80, 0x07, 0x37, 0xc4, 0x35, 0xa6, 0x29, 0x48,
		0x85, 0x70, 0x4f, 0x54, 0x1c, 0x41, 0x89, 0xaf, 0xf6, 0xbc,
		0xb5, 0x19, 0x85, 0x70, 0x4f, 0x54, 0x1c, 0x41, 0x89, 0xaf,
		0xf6, 0xbc, 0xb5, 0x19, 0xb5, 0x19, 0xb5, 0x19,
	];
	const expected: [_]u8 = [
		0x04, 0xb6, 0xb4, 0x77, 0x6e, 0x08, 0x86, 0x64, 0x55, 0x3a,
		0x64, 0xe0, 0xa3, 0xb0, 0x56, 0x39, 0x18, 0x4c, 0x5b, 0x15,
		0x3b, 0x19, 0xd0, 0x99, 0xc5, 0xd2, 0x7a, 0x4a, 0x23, 0xb9,
		0xf7, 0x37, 0x95, 0x4b, 0xe0, 0x97, 0x4c, 0x72, 0x16, 0x34,
		0x76, 0xf6, 0xce, 0x9e, 0xb7, 0x52, 0xc9, 0x33, 0xed, 0x26,
		0x5b, 0x7c, 0xb0, 0xd8, 0xe8, 0x8f, 0xc1, 0xb2, 0xd4, 0xbe,
		0xe3, 0x20, 0xc7, 0x82, 0xc7, 0x71, 0x3d, 0xaf, 0xad, 0xdb,
		0xe8, 0x56, 0xba, 0xad, 0x29, 0x76, 0x4c, 0x12, 0x24, 0xe9,
		0x59, 0xca, 0xef, 0x09, 0x48, 0x0d, 0x40, 0xb1, 0xce, 0x5f,
		0xcf, 0x6b, 0x62, 0xfd, 0xe5, 0x4b, 0xcc,
	];

	let g = alloc(P384_G);
	defer free(g);
	assert(c.mul(g, priv) == 1);
	assert(bytes::equal(g, expected));

	let r: [97]u8 = [0...];
	assert(c.mulgen(r, priv) == 97);
	assert(bytes::equal(r, expected));
};

@test fn p384_muladd() void = {
	let tcs = [
		multc {
			a = [
				0x04, 0xe4, 0x4f, 0x26, 0xd9, 0xce, 0xa7, 0xf0,
				0x69, 0xed, 0xa6, 0x1f, 0x01, 0xcb, 0xef, 0x53,
				0xe8, 0x2b, 0xf8, 0xdc, 0x16, 0x56, 0x05, 0x64,
				0x8a, 0x46, 0xf8, 0x54, 0x25, 0x8e, 0x3e, 0x6c,
				0xe8, 0xc3, 0x88, 0x0b, 0x30, 0xba, 0x9c, 0x8b,
				0xf7, 0xf8, 0x17, 0xbb, 0x34, 0x08, 0x4b, 0xc5,
				0xa7, 0xa3, 0x1d, 0x46, 0x64, 0x4d, 0xd1, 0xe5,
				0x68, 0x23, 0x49, 0x11, 0xbd, 0x4f, 0x21, 0x6f,
				0xcd, 0x88, 0x4d, 0xeb, 0x22, 0xad, 0x36, 0x57,
				0xf3, 0x94, 0x5b, 0x13, 0x3c, 0xf3, 0xf6, 0xc6,
				0xc6, 0x3c, 0xa1, 0x8e, 0xb6, 0x8c, 0x39, 0x85,
				0x01, 0x85, 0x22, 0x16, 0xe7, 0xba, 0x9a, 0xa1,
				0xd1,
			],
			b = [],
			x = [
				0xda, 0x9c, 0x84, 0xcf, 0x2d, 0x77, 0x4c, 0xfa,
				0xe5, 0xdc, 0x52, 0xc3, 0xb3, 0xda, 0x1c, 0xa0,
				0x26, 0xb0, 0x53, 0x16, 0x66, 0x46, 0x5f, 0x60,
				0xa6, 0xbe, 0x6e, 0xd1, 0x0a, 0x5f, 0x01, 0x50,
				0x63, 0xe1, 0x47, 0x62, 0x68, 0xcf, 0x40, 0x3a,
				0x2b, 0xf0, 0x96, 0x6c, 0x1e, 0x71, 0x24, 0x79,
			],
			y = [
				0xd9, 0xa7, 0x84, 0xd0, 0x77, 0x9e, 0x07, 0x4d,
				0x78, 0x09, 0x84, 0xef, 0x5b, 0x7d, 0x94, 0xd7,
				0xd8, 0x1f, 0x94, 0x1e, 0xfc, 0x3d, 0x76, 0x6c,
				0x0d, 0x4c, 0x87, 0x76, 0xdf, 0x52, 0xfc, 0xe0,
				0x02, 0x17, 0x2c, 0x95, 0x5e, 0x4b, 0xb7, 0x1f,
				0x84, 0xce, 0x2e, 0x54, 0xf2, 0x08, 0xf7, 0x6a,
			],
			result = 1,
			expected = [
				0x04, 0x69, 0xf9, 0xbb, 0x7a, 0x32, 0x63, 0xef,
				0xf2, 0xce, 0x3f, 0x4f, 0xf2, 0x48, 0x86, 0xd8,
				0xe4, 0xa1, 0x68, 0x1f, 0x7b, 0x6c, 0x24, 0xcf,
				0xa2, 0x60, 0x03, 0x0c, 0x8b, 0x10, 0x24, 0x4c,
				0xea, 0x6f, 0x47, 0xc3, 0x75, 0xb0, 0x04, 0x07,
				0xe0, 0xd1, 0x32, 0x8d, 0x02, 0x42, 0x75, 0x8c,
				0x67, 0x90, 0x19, 0x12, 0x26, 0xd3, 0xaf, 0x57,
				0x67, 0xc9, 0x20, 0x17, 0xd5, 0x2e, 0x57, 0xbb,
				0x78, 0x98, 0xe4, 0xb5, 0xd5, 0x65, 0x53, 0x78,
				0x20, 0x99, 0x2b, 0x43, 0x17, 0x1d, 0x2f, 0xdb,
				0x2c, 0xd5, 0xe5, 0x61, 0x22, 0x1f, 0xae, 0x22,
				0xae, 0x71, 0x03, 0x25, 0x9a, 0x47, 0x28, 0x4a,
				0x0f,
			],
		},
		multc {
			a = [
				0x04, 0xe4, 0x4f, 0x26, 0xd9, 0xce, 0xa7, 0xf0,
				0x69, 0xed, 0xa6, 0x1f, 0x01, 0xcb, 0xef, 0x53,
				0xe8, 0x2b, 0xf8, 0xdc, 0x16, 0x56, 0x05, 0x64,
				0x8a, 0x46, 0xf8, 0x54, 0x25, 0x8e, 0x3e, 0x6c,
				0xe8, 0xc3, 0x88, 0x0b, 0x30, 0xba, 0x9c, 0x8b,
				0xf7, 0xf8, 0x17, 0xbb, 0x34, 0x08, 0x4b, 0xc5,
				0xa7, 0xa3, 0x1d, 0x46, 0x64, 0x4d, 0xd1, 0xe5,
				0x68, 0x23, 0x49, 0x11, 0xbd, 0x4f, 0x21, 0x6f,
				0xcd, 0x88, 0x4d, 0xeb, 0x22, 0xad, 0x36, 0x57,
				0xf3, 0x94, 0x5b, 0x13, 0x3c, 0xf3, 0xf6, 0xc6,
				0xc6, 0x3c, 0xa1, 0x8e, 0xb6, 0x8c, 0x39, 0x85,
				0x01, 0x85, 0x22, 0x16, 0xe7, 0xba, 0x9a, 0xa1,
				0xd1,
			],
			b = [
				0x04, 0xe0, 0xb0, 0x1d, 0x37, 0xe0, 0x12, 0xba,
				0x21, 0xcd, 0xc5, 0xc4, 0x18, 0xfd, 0x85, 0x0a,
				0x21, 0x11, 0x32, 0x69, 0x73, 0xc0, 0xd4, 0x55,
				0xf3, 0x2e, 0x9d, 0x25, 0x0c, 0x4a, 0xc8, 0x89,
				0x4d, 0x7c, 0xbf, 0xca, 0x2d, 0x28, 0x6d, 0x20,
				0x5b, 0xbf, 0x0d, 0x1b, 0x6e, 0x92, 0x97, 0xd6,
				0xbb, 0xe2, 0x1b, 0x17, 0xac, 0xce, 0xd9, 0x7c,
				0x7e, 0x54, 0xca, 0xfb, 0xaf, 0x53, 0xa8, 0xde,
				0xba, 0x4c, 0x0a, 0x0f, 0x67, 0x03, 0xe7, 0x23,
				0xcf, 0x19, 0x07, 0x31, 0x71, 0x9a, 0x49, 0x51,
				0x7f, 0xbd, 0x5b, 0xa0, 0x34, 0x5f, 0x79, 0xba,
				0x48, 0xf1, 0x41, 0x9d, 0xcc, 0x2d, 0xef, 0x80,
				0xbb,
			],
			x = [
				0xda, 0x9c, 0x84, 0xcf, 0x2d, 0x77, 0x4c, 0xfa,
				0xe5, 0xdc, 0x52, 0xc3, 0xb3, 0xda, 0x1c, 0xa0,
				0x26, 0xb0, 0x53, 0x16, 0x66, 0x46, 0x5f, 0x60,
				0xa6, 0xbe, 0x6e, 0xd1, 0x0a, 0x5f, 0x01, 0x50,
				0x63, 0xe1, 0x47, 0x62, 0x68, 0xcf, 0x40, 0x3a,
				0x2b, 0xf0, 0x96, 0x6c, 0x1e, 0x71, 0x24, 0x79,
			],
			y = [
				0xd9, 0xa7, 0x84, 0xd0, 0x77, 0x9e, 0x07, 0x4d,
				0x78, 0x09, 0x84, 0xef, 0x5b, 0x7d, 0x94, 0xd7,
				0xd8, 0x1f, 0x94, 0x1e, 0xfc, 0x3d, 0x76, 0x6c,
				0x0d, 0x4c, 0x87, 0x76, 0xdf, 0x52, 0xfc, 0xe0,
				0x02, 0x17, 0x2c, 0x95, 0x5e, 0x4b, 0xb7, 0x1f,
				0x84, 0xce, 0x2e, 0x54, 0xf2, 0x08, 0xf7, 0x6a,
			],
			result = 1,
			expected = [
				0x04, 0xdc, 0x38, 0x1d, 0x38, 0x27, 0x1a, 0x83,
				0x03, 0x46, 0x18, 0xa6, 0xa7, 0xa7, 0x3e, 0xeb,
				0xa3, 0x3b, 0x9b, 0x05, 0x00, 0xe8, 0x09, 0xac,
				0x1a, 0x77, 0x5f, 0x21, 0xec, 0x5d, 0xe4, 0x70,
				0x3d, 0x18, 0x1f, 0x38, 0x5b, 0x5d, 0xaf, 0xed,
				0xc3, 0xff, 0xe4, 0x8a, 0xdb, 0x4b, 0x35, 0xfd,
				0x34, 0x2b, 0xfa, 0x29, 0x04, 0xe8, 0x55, 0x73,
				0xcf, 0xce, 0x1e, 0x2c, 0x34, 0x24, 0x08, 0xca,
				0x21, 0x71, 0xe1, 0xb4, 0x90, 0xd6, 0xe0, 0x60,
				0xff, 0x3b, 0x40, 0x70, 0xcd, 0x47, 0x26, 0xd1,
				0x5b, 0xc6, 0xbf, 0x30, 0x94, 0x40, 0x6f, 0x88,
				0x09, 0x12, 0xe7, 0x3e, 0x22, 0x88, 0x7e, 0x6e,
				0xc1,
			],
		},
		// invalid a
		multc {
			a = [
				0x04, 0xe4, 0x4f, 0x26, 0xd9, 0xce, 0xa7, 0xf0,
				0x69, 0xed, 0xa6, 0x1f, 0x01, 0xcb, 0xef, 0x53,
				0xe8, 0x2b, 0x08, 0xdc, 0x16, 0x56, 0x05, 0x64,
				0x8a, 0x46, 0x08, 0x54, 0x25, 0x8e, 0x3e, 0x6c,
				0xe8, 0xc3, 0xf8, 0x0b, 0x30, 0xba, 0x9c, 0x8b,
				0xf7, 0xf8, 0xf7, 0xbb, 0x34, 0x08, 0x4b, 0xc5,
				0xa7, 0xa3, 0xfd, 0x46, 0x64, 0x4d, 0xd1, 0xe5,
				0x68, 0x23, 0x49, 0x11, 0xbd, 0x4f, 0x21, 0x6f,
				0xcd, 0x88, 0x4d, 0xeb, 0x22, 0xad, 0x36, 0x57,
				0xf3, 0x94, 0x00, 0x13, 0x3c, 0xf3, 0xf6, 0xc6,
				0xc6, 0x3c, 0x00, 0x8e, 0xb6, 0x8c, 0x39, 0x85,
				0x01, 0x85, 0x00, 0x16, 0xe7, 0xba, 0x9a, 0xa1,
				0xd1,
			],
			b = [
				0x04, 0xe0, 0xb0, 0x1d, 0x37, 0xe0, 0x12, 0xba,
				0x21, 0xcd, 0xc5, 0xc4, 0x18, 0xfd, 0x85, 0x0a,
				0x21, 0x11, 0x32, 0x69, 0x73, 0xc0, 0xd4, 0x55,
				0xf3, 0x2e, 0x9d, 0x25, 0x0c, 0x4a, 0xc8, 0x89,
				0x4d, 0x7c, 0xbf, 0xca, 0x2d, 0x28, 0x6d, 0x20,
				0x5b, 0xbf, 0x0d, 0x1b, 0x6e, 0x92, 0x97, 0xd6,
				0xbb, 0xe2, 0x1b, 0x17, 0xac, 0xce, 0xd9, 0x7c,
				0x7e, 0x54, 0xca, 0xfb, 0xaf, 0x53, 0xa8, 0xde,
				0xba, 0x4c, 0x0a, 0x0f, 0x67, 0x03, 0xe7, 0x23,
				0xcf, 0x19, 0x07, 0x31, 0x71, 0x9a, 0x49, 0x51,
				0x7f, 0xbd, 0x5b, 0xa0, 0x34, 0x5f, 0x79, 0xba,
				0x48, 0xf1, 0x41, 0x9d, 0xcc, 0x2d, 0xef, 0x80,
				0xbb,
			],
			x = [
				0xda, 0x9c, 0x84, 0xcf, 0x2d, 0x77, 0x4c, 0xfa,
				0xe5, 0xdc, 0x52, 0xc3, 0xb3, 0xda, 0x1c, 0xa0,
				0x26, 0xb0, 0x53, 0x16, 0x66, 0x46, 0x5f, 0x60,
				0xa6, 0xbe, 0x6e, 0xd1, 0x0a, 0x5f, 0x01, 0x50,
				0x63, 0xe1, 0x47, 0x62, 0x68, 0xcf, 0x40, 0x3a,
				0x2b, 0xf0, 0x96, 0x6c, 0x1e, 0x71, 0x24, 0x79,
			],
			y = [
				0xd9, 0xa7, 0x84, 0xd0, 0x77, 0x9e, 0x07, 0x4d,
				0x78, 0x09, 0x84, 0xef, 0x5b, 0x7d, 0x94, 0xd7,
				0xd8, 0x1f, 0x94, 0x1e, 0xfc, 0x3d, 0x76, 0x6c,
				0x0d, 0x4c, 0x87, 0x76, 0xdf, 0x52, 0xfc, 0xe0,
				0x02, 0x17, 0x2c, 0x95, 0x5e, 0x4b, 0xb7, 0x1f,
				0x84, 0xce, 0x2e, 0x54, 0xf2, 0x08, 0xf7, 0x6a,
			],
			result = 0,
			expected = [],
		},
		// invalid b
		multc {
			a = [
				0x04, 0xe4, 0x4f, 0x26, 0xd9, 0xce, 0xa7, 0xf0,
				0x69, 0xed, 0xa6, 0x1f, 0x01, 0xcb, 0xef, 0x53,
				0xe8, 0x2b, 0xf8, 0xdc, 0x16, 0x56, 0x05, 0x64,
				0x8a, 0x46, 0xf8, 0x54, 0x25, 0x8e, 0x3e, 0x6c,
				0xe8, 0xc3, 0x88, 0x0b, 0x30, 0xba, 0x9c, 0x8b,
				0xf7, 0xf8, 0x17, 0xbb, 0x34, 0x08, 0x4b, 0xc5,
				0xa7, 0xa3, 0x1d, 0x46, 0x64, 0x4d, 0xd1, 0xe5,
				0x68, 0x23, 0x49, 0x11, 0xbd, 0x4f, 0x21, 0x6f,
				0xcd, 0x88, 0x4d, 0xeb, 0x22, 0xad, 0x36, 0x57,
				0xf3, 0x94, 0x5b, 0x13, 0x3c, 0xf3, 0xf6, 0xc6,
				0xc6, 0x3c, 0xa1, 0x8e, 0xb6, 0x8c, 0x39, 0x85,
				0x01, 0x85, 0x22, 0x16, 0xe7, 0xba, 0x9a, 0xa1,
				0xd1,
			],
			b = [
				0x04, 0xe0, 0xb0, 0x1d, 0x37, 0xe0, 0x12, 0xba,
				0x21, 0xcd, 0xc5, 0xc4, 0x18, 0xfd, 0x85, 0x0a,
				0x21, 0x11, 0x32, 0x69, 0x73, 0xc0, 0xd4, 0x55,
				0xf3, 0x2e, 0x9d, 0x25, 0x0c, 0x4a, 0xc8, 0x89,
				0x4d, 0x7c, 0xbf, 0xca, 0x2d, 0x28, 0x6d, 0x20,
				0x5b, 0x0f, 0x0d, 0x1b, 0x6e, 0x92, 0x97, 0xd6,
				0xbb, 0x0f, 0x1b, 0x17, 0xac, 0xce, 0xd9, 0x7c,
				0x7e, 0x0f, 0xca, 0xfb, 0xaf, 0x53, 0xa8, 0xde,
				0xba, 0x0f, 0x0a, 0x0f, 0x67, 0x03, 0xe7, 0x23,
				0xcf, 0x0f, 0x07, 0x31, 0x71, 0x9a, 0x49, 0x51,
				0x7f, 0x0f, 0x5b, 0xa0, 0x34, 0x5f, 0x79, 0xba,
				0x48, 0xff, 0x41, 0x9d, 0xcc, 0x2d, 0xef, 0x80,
				0xbb,
			],
			x = [
				0xda, 0x9c, 0x84, 0xcf, 0x2d, 0x77, 0x4c, 0xfa,
				0xe5, 0xdc, 0x52, 0xc3, 0xb3, 0xda, 0x1c, 0xa0,
				0x26, 0xb0, 0x53, 0x16, 0x66, 0x46, 0x5f, 0x60,
				0xa6, 0xbe, 0x6e, 0xd1, 0x0a, 0x5f, 0x01, 0x50,
				0x63, 0xe1, 0x47, 0x62, 0x68, 0xcf, 0x40, 0x3a,
				0x2b, 0xf0, 0x96, 0x6c, 0x1e, 0x71, 0x24, 0x79,
			],
			y = [
				0xd9, 0xa7, 0x84, 0xd0, 0x77, 0x9e, 0x07, 0x4d,
				0x78, 0x09, 0x84, 0xef, 0x5b, 0x7d, 0x94, 0xd7,
				0xd8, 0x1f, 0x94, 0x1e, 0xfc, 0x3d, 0x76, 0x6c,
				0x0d, 0x4c, 0x87, 0x76, 0xdf, 0x52, 0xfc, 0xe0,
				0x02, 0x17, 0x2c, 0x95, 0x5e, 0x4b, 0xb7, 0x1f,
				0x84, 0xce, 0x2e, 0x54, 0xf2, 0x08, 0xf7, 0x6a,
			],
			result = 0,
			expected = [],
		},
		// invalid a and b
		multc {
			a = [
				0x04, 0xe4, 0x4f, 0x26, 0xd9, 0xce, 0xa7, 0xf0,
				0x69, 0xed, 0xf6, 0x1f, 0x01, 0xcb, 0xef, 0x53,
				0xe8, 0x2b, 0xf8, 0xdc, 0x16, 0x56, 0x05, 0x64,
				0x8a, 0x46, 0xf8, 0x54, 0x25, 0x8e, 0x3e, 0x6c,
				0xe8, 0xc3, 0xf8, 0x0b, 0x30, 0xba, 0x9c, 0x8b,
				0xf7, 0xf8, 0xf7, 0xbb, 0x34, 0x08, 0x4b, 0xc5,
				0xa7, 0xa3, 0xfd, 0x46, 0x64, 0x4d, 0xd1, 0xe5,
				0x68, 0x23, 0xf9, 0x11, 0xbd, 0x4f, 0x21, 0x6f,
				0xcd, 0x88, 0xfd, 0xeb, 0x22, 0xad, 0x36, 0x57,
				0xf3, 0x94, 0xfb, 0x13, 0x3c, 0xf3, 0xf6, 0xc6,
				0xc6, 0x3c, 0xf1, 0x8e, 0xb6, 0x8c, 0x39, 0x85,
				0x01, 0x85, 0xf2, 0x16, 0xe7, 0xba, 0x9a, 0xa1,
				0xd1,
			],
			b = [
				0x04, 0xe0, 0xb0, 0x1d, 0x37, 0xe0, 0x12, 0xba,
				0x21, 0xcd, 0xc5, 0xc4, 0x18, 0xfd, 0x85, 0x0a,
				0x21, 0x11, 0x32, 0x60, 0x73, 0xc0, 0xd4, 0x55,
				0xf3, 0x2e, 0x9d, 0x20, 0x0c, 0x4a, 0xc8, 0x89,
				0x4d, 0x7c, 0xbf, 0xc0, 0x2d, 0x28, 0x6d, 0x20,
				0x5b, 0xbf, 0x0d, 0x10, 0x6e, 0x92, 0x97, 0xd6,
				0xbb, 0xe2, 0x1b, 0x10, 0xac, 0xce, 0xd9, 0x7c,
				0x7e, 0x54, 0xca, 0xf0, 0xaf, 0x53, 0xa8, 0xde,
				0xba, 0x4c, 0x0a, 0x00, 0x67, 0x03, 0xe7, 0x23,
				0xcf, 0x19, 0x07, 0x30, 0x71, 0x9a, 0x49, 0x51,
				0x7f, 0xbd, 0x5b, 0xa0, 0x34, 0x5f, 0x79, 0xba,
				0x48, 0xf1, 0x41, 0x90, 0xcc, 0x2d, 0xef, 0x80,
				0xbb,
			],
			x = [
				0xda, 0x9c, 0x84, 0xcf, 0x2d, 0x77, 0x4c, 0xfa,
				0xe5, 0xdc, 0x52, 0xc3, 0xb3, 0xda, 0x1c, 0xa0,
				0x26, 0xb0, 0x53, 0x16, 0x66, 0x46, 0x5f, 0x60,
				0xa6, 0xbe, 0x6e, 0xd1, 0x0a, 0x5f, 0x01, 0x50,
				0x63, 0xe1, 0x47, 0x62, 0x68, 0xcf, 0x40, 0x3a,
				0x2b, 0xf0, 0x96, 0x6c, 0x1e, 0x71, 0x24, 0x79,
			],
			y = [
				0xd9, 0xa7, 0x84, 0xd0, 0x77, 0x9e, 0x07, 0x4d,
				0x78, 0x09, 0x84, 0xef, 0x5b, 0x7d, 0x94, 0xd7,
				0xd8, 0x1f, 0x94, 0x1e, 0xfc, 0x3d, 0x76, 0x6c,
				0x0d, 0x4c, 0x87, 0x76, 0xdf, 0x52, 0xfc, 0xe0,
				0x02, 0x17, 0x2c, 0x95, 0x5e, 0x4b, 0xb7, 0x1f,
				0x84, 0xce, 0x2e, 0x54, 0xf2, 0x08, 0xf7, 0x6a,
			],
			result = 0,
			expected = [],
		},
	];

	tmuladd(p384, tcs);
};

@test fn p521_mulgen() void = {
	let c = p521;

	// multiply by 1
	let m1: [P521_SCALARSZ]u8 = [0...];
	m1[len(m1) - 1] = 1;

	let g = alloc(P521_G);
	defer free(g);
	assert(c.mul(g, m1) == 1);
	assert(bytes::equal(g, P521_G));

	assert(c.mulgen(g, m1) == 133);
	assert(bytes::equal(g, P521_G));

	// multiply by order - 1
	let o = alloc(P521_N);
	defer free(o);
	o[len(o) - 1] -= 1;

	const expected: [_]u8 = [
		0x04, 0x00, 0xc6, 0x85, 0x8e, 0x06, 0xb7, 0x04, 0x04, 0xe9,
		0xcd, 0x9e, 0x3e, 0xcb, 0x66, 0x23, 0x95, 0xb4, 0x42, 0x9c,
		0x64, 0x81, 0x39, 0x05, 0x3f, 0xb5, 0x21, 0xf8, 0x28, 0xaf,
		0x60, 0x6b, 0x4d, 0x3d, 0xba, 0xa1, 0x4b, 0x5e, 0x77, 0xef,
		0xe7, 0x59, 0x28, 0xfe, 0x1d, 0xc1, 0x27, 0xa2, 0xff, 0xa8,
		0xde, 0x33, 0x48, 0xb3, 0xc1, 0x85, 0x6a, 0x42, 0x9b, 0xf9,
		0x7e, 0x7e, 0x31, 0xc2, 0xe5, 0xbd, 0x66, 0x00, 0xe7, 0xc6,
		0xd6, 0x95, 0x87, 0x65, 0xc4, 0x3f, 0xfb, 0xa3, 0x75, 0xa0,
		0x4b, 0xd3, 0x82, 0xe4, 0x26, 0x67, 0x0a, 0xbb, 0xb6, 0xa8,
		0x64, 0xbb, 0x97, 0xe8, 0x50, 0x42, 0xe8, 0xd8, 0xc1, 0x99,
		0xd3, 0x68, 0x11, 0x8d, 0x66, 0xa1, 0x0b, 0xd9, 0xbf, 0x3a,
		0xaf, 0x46, 0xfe, 0xc0, 0x52, 0xf8, 0x9e, 0xca, 0xc3, 0x8f,
		0x79, 0x5d, 0x8d, 0x3d, 0xbf, 0x77, 0x41, 0x6b, 0x89, 0x60,
		0x2e, 0x99, 0xaf,
	];

	assert(c.mul(g, o) == 1);
	assert(bytes::equal(g, expected));

	let g = alloc(P521_G);
	defer free(g);
	assert(c.mulgen(g, o) == 133);
	assert(bytes::equal(g, expected));

	// random example
	let priv: [_]u8 = [
		0x01, 0x1f, 0xd5, 0x29, 0xff, 0x28, 0x24, 0x93, 0x43, 0x23,
		0x3c, 0x0f, 0xe4, 0x15, 0x6a, 0xea, 0x1c, 0x95, 0xe6, 0xf1,
		0x5e, 0x19, 0x30, 0xfe, 0xb4, 0xea, 0x0d, 0x24, 0xb3, 0x67,
		0xa0, 0xeb, 0xcf, 0xed, 0x5c, 0xcf, 0xcf, 0x9f, 0x9d, 0x48,
		0x04, 0x8b, 0xb1, 0x56, 0x53, 0xba, 0xa4, 0x86, 0x01, 0x29,
		0x61, 0x21, 0xca, 0xd7, 0x63, 0x84, 0x35, 0x45, 0xc3, 0x9b,
		0x55, 0xff, 0x9a, 0x4f, 0x27, 0x03,
	];
	const expected: [_]u8 = [
		0x04, 0x00, 0x9f, 0x72, 0x15, 0x21, 0x0c, 0xdd, 0x22, 0x24,
		0x16, 0xbb, 0x18, 0xa6, 0x46, 0x21, 0xee, 0x4f, 0x73, 0x1e,
		0x9e, 0x03, 0xaf, 0x79, 0xc7, 0x1f, 0xf0, 0x33, 0x1c, 0xa6,
		0xa1, 0x13, 0x98, 0x95, 0x6e, 0x88, 0x44, 0x6b, 0xa4, 0xbd,
		0x4d, 0x49, 0x9e, 0xcb, 0x96, 0x0e, 0xdf, 0x67, 0x89, 0xca,
		0xd5, 0xc3, 0x9e, 0x99, 0xa8, 0xc9, 0x00, 0x4b, 0x2c, 0x49,
		0x57, 0xd7, 0xee, 0xf4, 0x6e, 0x07, 0xbb, 0x01, 0x35, 0xe2,
		0xa4, 0xd6, 0x60, 0x99, 0x2c, 0x62, 0xad, 0xab, 0x89, 0xb5,
		0xe7, 0xb6, 0x4b, 0x44, 0x89, 0xcf, 0xb0, 0x19, 0x29, 0xf8,
		0x54, 0x76, 0x07, 0x40, 0x3a, 0xee, 0x41, 0x0c, 0x64, 0xeb,
		0xda, 0x1e, 0x22, 0x4a, 0xee, 0xdb, 0x16, 0xa0, 0x5d, 0xe1,
		0x95, 0x2f, 0xe3, 0xbc, 0x53, 0xd3, 0xf4, 0x36, 0x76, 0x03,
		0x1f, 0xd8, 0xc6, 0xf4, 0x23, 0x4f, 0x45, 0x4e, 0x11, 0xeb,
		0xb2, 0xb1, 0x62,
	];

	let g = alloc(P521_G);
	defer free(g);
	assert(c.mul(g, priv) == 1);
	assert(bytes::equal(g, expected));

	let r: [133]u8 = [0...];
	assert(c.mulgen(r, priv) == 133);
	assert(bytes::equal(r, expected));
};

@test fn p521_muladd() void = {
	let tcs = [
		multc {
			a = [
				0x04, 0x01, 0xb1, 0x0a, 0x39, 0x7f, 0x94, 0xe9,
				0x0b, 0x4f, 0x8f, 0xf1, 0xe8, 0x31, 0xca, 0x0a,
				0xda, 0x8f, 0x1c, 0x80, 0x1e, 0x1a, 0x95, 0x65,
				0xdb, 0x0f, 0x52, 0x7f, 0xaa, 0x14, 0x65, 0x6d,
				0xe9, 0xe6, 0x5c, 0xa2, 0x34, 0xc8, 0xea, 0x11,
				0x67, 0x4a, 0xc3, 0x5f, 0xce, 0x8b, 0xa8, 0xe8,
				0xe4, 0x8b, 0x6c, 0x9e, 0x5c, 0x0d, 0x37, 0xf0,
				0x4e, 0x33, 0xb8, 0xd1, 0x5f, 0xce, 0x90, 0x92,
				0xa7, 0x14, 0x07, 0x01, 0x1e, 0xde, 0x56, 0x92,
				0xcc, 0x39, 0xb6, 0xe9, 0xcf, 0xbe, 0xe0, 0xeb,
				0x35, 0x30, 0xfc, 0xf2, 0x2b, 0xc6, 0xe4, 0xfa,
				0x71, 0x2e, 0x2f, 0x87, 0x54, 0x83, 0xb1, 0x9c,
				0x96, 0xe2, 0xbb, 0x72, 0xca, 0x51, 0xf2, 0x58,
				0x25, 0x80, 0xf4, 0x47, 0xa3, 0xb6, 0x29, 0x45,
				0x3c, 0x28, 0xa7, 0x65, 0x85, 0x40, 0xb7, 0x2b,
				0x75, 0x38, 0x4a, 0x3e, 0x25, 0x0a, 0xb6, 0x58,
				0xe3, 0x9a, 0x7c, 0xde, 0xd5,
			],
			b = [],
			x = [
				0x01, 0x74, 0xe2, 0x0e, 0x0c, 0xa6, 0xd0, 0x12,
				0xb0, 0xc3, 0x86, 0xbc, 0xfc, 0x9a, 0xcb, 0x09,
				0x7a, 0xf9, 0xca, 0xb7, 0xc8, 0x79, 0x39, 0x3e,
				0xb6, 0x8e, 0x3e, 0x2f, 0x02, 0x6a, 0xfd, 0x07,
				0x65, 0xe9, 0x97, 0xe1, 0xf5, 0xf0, 0x16, 0x9a,
				0xa2, 0xe6, 0x03, 0x75, 0x1f, 0xa2, 0xf4, 0xe5,
				0xcd, 0x54, 0x94, 0x60, 0xb2, 0xfd, 0xe7, 0x97,
				0xea, 0x72, 0x02, 0xb9, 0x96, 0x48, 0xd3, 0x45,
				0xc2, 0x26,
			],
			y = [
				0x01, 0xae, 0xa0, 0x8f, 0x5b, 0x13, 0xe1, 0x85,
				0x4f, 0xfe, 0xcf, 0x73, 0x6a, 0x18, 0xc3, 0xfa,
				0xb7, 0xe6, 0xfc, 0xe5, 0xa9, 0x09, 0x8a, 0x68,
				0x4f, 0x49, 0x93, 0x59, 0xeb, 0xfd, 0x91, 0xf9,
				0x45, 0x1d, 0xcf, 0x51, 0x61, 0x39, 0x5c, 0x87,
				0x6c, 0x70, 0x9d, 0xfa, 0x7a, 0x86, 0x30, 0x64,
				0x3a, 0x4f, 0x48, 0x78, 0x3a, 0x2f, 0x9f, 0x84,
				0x07, 0xc1, 0x94, 0x5a, 0xc7, 0x1a, 0xe2, 0x5d,
				0x73, 0xb3,
			],
			result = 1,
			expected = [
				0x04, 0x00, 0x27, 0xd3, 0x90, 0xf4, 0xf7, 0xdc,
				0x2a, 0x67, 0xa0, 0x2b, 0x8c, 0x31, 0x3b, 0xe3,
				0x37, 0xb9, 0xf9, 0x08, 0x49, 0x46, 0x56, 0xa6,
				0xa4, 0x3d, 0x7c, 0x0a, 0x74, 0x98, 0x72, 0x20,
				0xbe, 0xa7, 0xf8, 0x67, 0x95, 0x7d, 0x1f, 0x6a,
				0x38, 0x03, 0xc9, 0xf3, 0xac, 0x55, 0xb9, 0x5b,
				0x5d, 0xeb, 0x01, 0xe0, 0xaf, 0xf1, 0x66, 0xcf,
				0x90, 0xe3, 0x43, 0x5c, 0x25, 0xfb, 0xcd, 0x48,
				0xd4, 0xf5, 0xbd, 0x01, 0xb5, 0xa5, 0xd4, 0xa1,
				0xe4, 0x4f, 0xab, 0x94, 0x96, 0xdd, 0x32, 0x7f,
				0x9e, 0x40, 0x1c, 0x25, 0x7d, 0xcb, 0xed, 0xa5,
				0x53, 0xac, 0x4f, 0xa3, 0x72, 0x75, 0x56, 0xd8,
				0x32, 0x2c, 0x76, 0xed, 0x0d, 0xe7, 0x5c, 0xbd,
				0xbd, 0xe8, 0x09, 0x35, 0x0e, 0x57, 0xd4, 0x20,
				0xe1, 0x46, 0x6c, 0x49, 0xe1, 0x49, 0xc6, 0x04,
				0x6e, 0xf9, 0xc9, 0x87, 0x76, 0x0a, 0x9a, 0x3b,
				0x2b, 0xa6, 0x43, 0xc2, 0x24,
			],
		},
		multc {
			a = [
				0x04, 0x01, 0xb1, 0x0a, 0x39, 0x7f, 0x94, 0xe9,
				0x0b, 0x4f, 0x8f, 0xf1, 0xe8, 0x31, 0xca, 0x0a,
				0xda, 0x8f, 0x1c, 0x80, 0x1e, 0x1a, 0x95, 0x65,
				0xdb, 0x0f, 0x52, 0x7f, 0xaa, 0x14, 0x65, 0x6d,
				0xe9, 0xe6, 0x5c, 0xa2, 0x34, 0xc8, 0xea, 0x11,
				0x67, 0x4a, 0xc3, 0x5f, 0xce, 0x8b, 0xa8, 0xe8,
				0xe4, 0x8b, 0x6c, 0x9e, 0x5c, 0x0d, 0x37, 0xf0,
				0x4e, 0x33, 0xb8, 0xd1, 0x5f, 0xce, 0x90, 0x92,
				0xa7, 0x14, 0x07, 0x01, 0x1e, 0xde, 0x56, 0x92,
				0xcc, 0x39, 0xb6, 0xe9, 0xcf, 0xbe, 0xe0, 0xeb,
				0x35, 0x30, 0xfc, 0xf2, 0x2b, 0xc6, 0xe4, 0xfa,
				0x71, 0x2e, 0x2f, 0x87, 0x54, 0x83, 0xb1, 0x9c,
				0x96, 0xe2, 0xbb, 0x72, 0xca, 0x51, 0xf2, 0x58,
				0x25, 0x80, 0xf4, 0x47, 0xa3, 0xb6, 0x29, 0x45,
				0x3c, 0x28, 0xa7, 0x65, 0x85, 0x40, 0xb7, 0x2b,
				0x75, 0x38, 0x4a, 0x3e, 0x25, 0x0a, 0xb6, 0x58,
				0xe3, 0x9a, 0x7c, 0xde, 0xd5,
			],
			b = [
				0x04, 0x01, 0x01, 0xbf, 0xd2, 0xa7, 0xed, 0xe7,
				0x68, 0x5a, 0x86, 0x4b, 0xc8, 0x40, 0x42, 0x9d,
				0xea, 0x5c, 0xe5, 0x3e, 0x3d, 0x3d, 0x48, 0xc6,
				0x38, 0xb0, 0x7f, 0xd4, 0x35, 0x67, 0x67, 0xb2,
				0x12, 0xa7, 0xba, 0xd4, 0xc3, 0x22, 0x3f, 0x7d,
				0xff, 0xe2, 0x23, 0x8e, 0x72, 0x71, 0x2c, 0x24,
				0xd5, 0x91, 0xdc, 0x9c, 0xb2, 0xc7, 0x2d, 0x5c,
				0xe1, 0xf7, 0x17, 0x49, 0x09, 0xeb, 0xe4, 0x26,
				0xab, 0xe6, 0x6d, 0x01, 0x26, 0xb1, 0x8e, 0x19,
				0xb3, 0xe3, 0x72, 0xe9, 0xf4, 0x15, 0xe3, 0x52,
				0x2a, 0xb3, 0xcb, 0xac, 0xf8, 0xe7, 0xc0, 0x14,
				0x60, 0x97, 0x71, 0xa0, 0x54, 0x2b, 0x94, 0x4c,
				0x13, 0x6e, 0xfb, 0x98, 0x11, 0x72, 0x60, 0x6a,
				0x0e, 0xd5, 0xb4, 0xe8, 0x17, 0x6c, 0x09, 0x2a,
				0xff, 0x89, 0xac, 0x88, 0xcd, 0x56, 0xe4, 0xcc,
				0x66, 0x79, 0x8c, 0xb7, 0xd2, 0x44, 0x44, 0x60,
				0xd8, 0x04, 0xa2, 0x50, 0x0f,
			],
			x = [
				0x01, 0x74, 0xe2, 0x0e, 0x0c, 0xa6, 0xd0, 0x12,
				0xb0, 0xc3, 0x86, 0xbc, 0xfc, 0x9a, 0xcb, 0x09,
				0x7a, 0xf9, 0xca, 0xb7, 0xc8, 0x79, 0x39, 0x3e,
				0xb6, 0x8e, 0x3e, 0x2f, 0x02, 0x6a, 0xfd, 0x07,
				0x65, 0xe9, 0x97, 0xe1, 0xf5, 0xf0, 0x16, 0x9a,
				0xa2, 0xe6, 0x03, 0x75, 0x1f, 0xa2, 0xf4, 0xe5,
				0xcd, 0x54, 0x94, 0x60, 0xb2, 0xfd, 0xe7, 0x97,
				0xea, 0x72, 0x02, 0xb9, 0x96, 0x48, 0xd3, 0x45,
				0xc2, 0x26,
			],
			y = [
				0x01, 0xae, 0xa0, 0x8f, 0x5b, 0x13, 0xe1, 0x85,
				0x4f, 0xfe, 0xcf, 0x73, 0x6a, 0x18, 0xc3, 0xfa,
				0xb7, 0xe6, 0xfc, 0xe5, 0xa9, 0x09, 0x8a, 0x68,
				0x4f, 0x49, 0x93, 0x59, 0xeb, 0xfd, 0x91, 0xf9,
				0x45, 0x1d, 0xcf, 0x51, 0x61, 0x39, 0x5c, 0x87,
				0x6c, 0x70, 0x9d, 0xfa, 0x7a, 0x86, 0x30, 0x64,
				0x3a, 0x4f, 0x48, 0x78, 0x3a, 0x2f, 0x9f, 0x84,
				0x07, 0xc1, 0x94, 0x5a, 0xc7, 0x1a, 0xe2, 0x5d,
				0x73, 0xb3,
			],
			result = 1,
			expected = [
				0x04, 0x01, 0x4b, 0x18, 0x4d, 0x28, 0xd0, 0x8f,
				0x23, 0xfc, 0x27, 0x13, 0x7e, 0xc9, 0x9d, 0xb2,
				0x39, 0xb5, 0x5b, 0x7a, 0x30, 0xcb, 0x35, 0x47,
				0x1b, 0x04, 0x63, 0x8a, 0x50, 0x15, 0xb2, 0x79,
				0x9d, 0x74, 0xf5, 0xbc, 0x21, 0x14, 0x5e, 0x9f,
				0x3f, 0x88, 0xfe, 0x46, 0x9d, 0x7c, 0xb8, 0x1a,
				0x1c, 0x5c, 0x86, 0x60, 0xf2, 0xbb, 0x04, 0xdc,
				0x81, 0x9d, 0xf3, 0x35, 0x75, 0x5e, 0xa9, 0x58,
				0x36, 0x17, 0x33, 0x01, 0x86, 0xd9, 0x16, 0xdb,
				0x10, 0xae, 0x45, 0x0a, 0xb6, 0x75, 0x6d, 0x90,
				0x85, 0x92, 0xde, 0x4c, 0x96, 0x3b, 0xfd, 0x31,
				0xe6, 0x99, 0x7f, 0xe2, 0xb6, 0xfd, 0xbb, 0x76,
				0xe4, 0x62, 0x8f, 0xb8, 0xba, 0x8c, 0x9d, 0xc9,
				0xc4, 0x2f, 0x3e, 0x67, 0xd5, 0xaf, 0xbc, 0xf0,
				0x60, 0x8c, 0xca, 0xec, 0xa9, 0x21, 0xd1, 0x8e,
				0x29, 0xc9, 0x81, 0x76, 0xdb, 0x17, 0x17, 0xd8,
				0x6b, 0x97, 0x58, 0x88, 0x78,
			],
		},
		// invalid a
		multc {
			a = [
				0x04, 0x01, 0xb1, 0x0a, 0x39, 0x7f, 0x94, 0xe9,
				0x0b, 0x4f, 0x8f, 0xf1, 0xe8, 0x31, 0xca, 0x0a,
				0xda, 0x8f, 0x1c, 0x80, 0x1e, 0x1a, 0x95, 0x65,
				0xdb, 0x0f, 0x52, 0x7f, 0xaa, 0x14, 0x65, 0x6d,
				0xe9, 0xef, 0x5c, 0xa2, 0x34, 0xc8, 0xea, 0x11,
				0x67, 0x4f, 0xc3, 0x5f, 0xce, 0x8b, 0xa8, 0xe8,
				0xe4, 0x8f, 0x6c, 0x9e, 0x5c, 0x0d, 0x37, 0xf0,
				0x4e, 0x3f, 0xb8, 0xd1, 0x5f, 0xce, 0x90, 0x92,
				0xa7, 0x1f, 0x07, 0x01, 0x1e, 0xde, 0x56, 0x92,
				0xcc, 0x3f, 0xb6, 0xe9, 0xcf, 0xbe, 0xe0, 0xeb,
				0x35, 0x3f, 0xfc, 0xf2, 0x2b, 0xc6, 0xe4, 0xfa,
				0x71, 0x2f, 0x2f, 0x87, 0x54, 0x83, 0xb1, 0x9c,
				0x96, 0xef, 0xbb, 0x72, 0xca, 0x51, 0xf2, 0x58,
				0x25, 0x8f, 0xf4, 0x47, 0xa3, 0xb6, 0x29, 0x45,
				0x3c, 0x2f, 0xa7, 0x65, 0x85, 0x40, 0xb7, 0x2b,
				0x75, 0x38, 0x4a, 0x3e, 0x25, 0x0a, 0xb6, 0x58,
				0xe3, 0x9a, 0x7c, 0xde, 0xd5,
			],
			b = [
				0x04, 0x01, 0x01, 0xbf, 0xd2, 0xa7, 0xed, 0xe7,
				0x68, 0x5a, 0x86, 0x4b, 0xc8, 0x40, 0x42, 0x9d,
				0xea, 0x5c, 0xe5, 0x3e, 0x3d, 0x3d, 0x48, 0xc6,
				0x38, 0xb0, 0x7f, 0xd4, 0x35, 0x67, 0x67, 0xb2,
				0x12, 0xa7, 0xba, 0xd4, 0xc3, 0x22, 0x3f, 0x7d,
				0xff, 0xe2, 0x23, 0x8e, 0x72, 0x71, 0x2c, 0x24,
				0xd5, 0x91, 0xdc, 0x9c, 0xb2, 0xc7, 0x2d, 0x5c,
				0xe1, 0xf7, 0x17, 0x49, 0x09, 0xeb, 0xe4, 0x26,
				0xab, 0xe6, 0x6d, 0x01, 0x26, 0xb1, 0x8e, 0x19,
				0xb3, 0xe3, 0x72, 0xe9, 0xf4, 0x15, 0xe3, 0x52,
				0x2a, 0xb3, 0xcb, 0xac, 0xf8, 0xe7, 0xc0, 0x14,
				0x60, 0x97, 0x71, 0xa0, 0x54, 0x2b, 0x94, 0x4c,
				0x13, 0x6e, 0xfb, 0x98, 0x11, 0x72, 0x60, 0x6a,
				0x0e, 0xd5, 0xb4, 0xe8, 0x17, 0x6c, 0x09, 0x2a,
				0xff, 0x89, 0xac, 0x88, 0xcd, 0x56, 0xe4, 0xcc,
				0x66, 0x79, 0x8c, 0xb7, 0xd2, 0x44, 0x44, 0x60,
				0xd8, 0x04, 0xa2, 0x50, 0x0f,
			],
			x = [
				0x01, 0x74, 0xe2, 0x0e, 0x0c, 0xa6, 0xd0, 0x12,
				0xb0, 0xc3, 0x86, 0xbc, 0xfc, 0x9a, 0xcb, 0x09,
				0x7a, 0xf9, 0xca, 0xb7, 0xc8, 0x79, 0x39, 0x3e,
				0xb6, 0x8e, 0x3e, 0x2f, 0x02, 0x6a, 0xfd, 0x07,
				0x65, 0xe9, 0x97, 0xe1, 0xf5, 0xf0, 0x16, 0x9a,
				0xa2, 0xe6, 0x03, 0x75, 0x1f, 0xa2, 0xf4, 0xe5,
				0xcd, 0x54, 0x94, 0x60, 0xb2, 0xfd, 0xe7, 0x97,
				0xea, 0x72, 0x02, 0xb9, 0x96, 0x48, 0xd3, 0x45,
				0xc2, 0x26,
			],
			y = [
				0x01, 0xae, 0xa0, 0x8f, 0x5b, 0x13, 0xe1, 0x85,
				0x4f, 0xfe, 0xcf, 0x73, 0x6a, 0x18, 0xc3, 0xfa,
				0xb7, 0xe6, 0xfc, 0xe5, 0xa9, 0x09, 0x8a, 0x68,
				0x4f, 0x49, 0x93, 0x59, 0xeb, 0xfd, 0x91, 0xf9,
				0x45, 0x1d, 0xcf, 0x51, 0x61, 0x39, 0x5c, 0x87,
				0x6c, 0x70, 0x9d, 0xfa, 0x7a, 0x86, 0x30, 0x64,
				0x3a, 0x4f, 0x48, 0x78, 0x3a, 0x2f, 0x9f, 0x84,
				0x07, 0xc1, 0x94, 0x5a, 0xc7, 0x1a, 0xe2, 0x5d,
				0x73, 0xb3,
			],
			result = 0,
			expected = [],
		},
		// invalid b
		multc {
			a = [
				0x04, 0x01, 0xb1, 0x0a, 0x39, 0x7f, 0x94, 0xe9,
				0x0b, 0x4f, 0x8f, 0xf1, 0xe8, 0x31, 0xca, 0x0a,
				0xda, 0x8f, 0x1c, 0x80, 0x1e, 0x1a, 0x95, 0x65,
				0xdb, 0x0f, 0x52, 0x7f, 0xaa, 0x14, 0x65, 0x6d,
				0xe9, 0xe6, 0x5c, 0xa2, 0x34, 0xc8, 0xea, 0x11,
				0x67, 0x4a, 0xc3, 0x5f, 0xce, 0x8b, 0xa8, 0xe8,
				0xe4, 0x8b, 0x6c, 0x9e, 0x5c, 0x0d, 0x37, 0xf0,
				0x4e, 0x33, 0xb8, 0xd1, 0x5f, 0xce, 0x90, 0x92,
				0xa7, 0x14, 0x07, 0x01, 0x1e, 0xde, 0x56, 0x92,
				0xcc, 0x39, 0xb6, 0xe9, 0xcf, 0xbe, 0xe0, 0xeb,
				0x35, 0x30, 0xfc, 0xf2, 0x2b, 0xc6, 0xe4, 0xfa,
				0x71, 0x2e, 0x2f, 0x87, 0x54, 0x83, 0xb1, 0x9c,
				0x96, 0xe2, 0xbb, 0x72, 0xca, 0x51, 0xf2, 0x58,
				0x25, 0x80, 0xf4, 0x47, 0xa3, 0xb6, 0x29, 0x45,
				0x3c, 0x28, 0xa7, 0x65, 0x85, 0x40, 0xb7, 0x2b,
				0x75, 0x38, 0x4a, 0x3e, 0x25, 0x0a, 0xb6, 0x58,
				0xe3, 0x9a, 0x7c, 0xde, 0xd5,
			],
			b = [
				0x04, 0x01, 0x01, 0xbf, 0xd2, 0xa7, 0xed, 0xe7,
				0x68, 0x5a, 0x86, 0x4b, 0xc8, 0x40, 0x42, 0x9d,
				0xea, 0x5c, 0xe5, 0x5e, 0x3d, 0x3d, 0x48, 0xc6,
				0x38, 0xb0, 0x7f, 0x54, 0x35, 0x67, 0x67, 0xb2,
				0x12, 0xa7, 0xba, 0x54, 0xc3, 0x22, 0x3f, 0x7d,
				0xff, 0xe2, 0x23, 0x5e, 0x72, 0x71, 0x2c, 0x24,
				0xd5, 0x91, 0xdc, 0x5c, 0xb2, 0xc7, 0x2d, 0x5c,
				0xe1, 0xf7, 0x17, 0x59, 0x09, 0xeb, 0xe4, 0x26,
				0xab, 0xe6, 0x6d, 0x51, 0x26, 0xb1, 0x8e, 0x19,
				0xb3, 0xe3, 0x72, 0x59, 0xf4, 0x15, 0xe3, 0x52,
				0x2a, 0xb3, 0xcb, 0x5c, 0xf8, 0xe7, 0xc0, 0x14,
				0x60, 0x97, 0x71, 0x50, 0x54, 0x2b, 0x94, 0x4c,
				0x13, 0x6e, 0xfb, 0x58, 0x11, 0x72, 0x60, 0x6a,
				0x0e, 0xd5, 0xb4, 0x58, 0x17, 0x6c, 0x09, 0x2a,
				0xff, 0x89, 0xac, 0x58, 0xcd, 0x56, 0xe4, 0xcc,
				0x66, 0x79, 0x8c, 0xb7, 0xd2, 0x44, 0x44, 0x60,
				0xd8, 0x04, 0xa2, 0x50, 0x0f,
			],
			x = [
				0x01, 0x74, 0xe2, 0x0e, 0x0c, 0xa6, 0xd0, 0x12,
				0xb0, 0xc3, 0x86, 0xbc, 0xfc, 0x9a, 0xcb, 0x09,
				0x7a, 0xf9, 0xca, 0xb7, 0xc8, 0x79, 0x39, 0x3e,
				0xb6, 0x8e, 0x3e, 0x2f, 0x02, 0x6a, 0xfd, 0x07,
				0x65, 0xe9, 0x97, 0xe1, 0xf5, 0xf0, 0x16, 0x9a,
				0xa2, 0xe6, 0x03, 0x75, 0x1f, 0xa2, 0xf4, 0xe5,
				0xcd, 0x54, 0x94, 0x60, 0xb2, 0xfd, 0xe7, 0x97,
				0xea, 0x72, 0x02, 0xb9, 0x96, 0x48, 0xd3, 0x45,
				0xc2, 0x26,
			],
			y = [
				0x01, 0xae, 0xa0, 0x8f, 0x5b, 0x13, 0xe1, 0x85,
				0x4f, 0xfe, 0xcf, 0x73, 0x6a, 0x18, 0xc3, 0xfa,
				0xb7, 0xe6, 0xfc, 0xe5, 0xa9, 0x09, 0x8a, 0x68,
				0x4f, 0x49, 0x93, 0x59, 0xeb, 0xfd, 0x91, 0xf9,
				0x45, 0x1d, 0xcf, 0x51, 0x61, 0x39, 0x5c, 0x87,
				0x6c, 0x70, 0x9d, 0xfa, 0x7a, 0x86, 0x30, 0x64,
				0x3a, 0x4f, 0x48, 0x78, 0x3a, 0x2f, 0x9f, 0x84,
				0x07, 0xc1, 0x94, 0x5a, 0xc7, 0x1a, 0xe2, 0x5d,
				0x73, 0xb3,
			],
			result = 0,
			expected = [],
		},
		// invalid a and b
		multc {
			a = [
				0x04, 0x01, 0xb1, 0x0a, 0x39, 0x7f, 0x94, 0xe9,
				0x0b, 0x4f, 0x8f, 0xf1, 0xe8, 0x31, 0xca, 0x0a,
				0xda, 0x8f, 0xfc, 0x80, 0x1e, 0x1a, 0x95, 0x65,
				0xdb, 0x0f, 0xf2, 0x7f, 0xaa, 0x14, 0x65, 0x6d,
				0xe9, 0xe6, 0xfc, 0xa2, 0x34, 0xc8, 0xea, 0x11,
				0x67, 0x4a, 0xf3, 0x5f, 0xce, 0x8b, 0xa8, 0xe8,
				0xe4, 0x8b, 0xfc, 0x9e, 0x5c, 0x0d, 0x37, 0xf0,
				0x4e, 0x33, 0xf8, 0xd1, 0x5f, 0xce, 0x90, 0x92,
				0xa7, 0x14, 0xf7, 0x01, 0x1e, 0xde, 0x56, 0x92,
				0xcc, 0x39, 0xf6, 0xe9, 0xcf, 0xbe, 0xe0, 0xeb,
				0x35, 0x30, 0xfc, 0xf2, 0x2b, 0xc6, 0xe4, 0xfa,
				0x71, 0x2e, 0xff, 0x87, 0x54, 0x83, 0xb1, 0x9c,
				0x96, 0xe2, 0xfb, 0x72, 0xca, 0x51, 0xf2, 0x58,
				0x25, 0x80, 0xf4, 0x47, 0xa3, 0xb6, 0x29, 0x45,
				0x3c, 0x28, 0xf7, 0x65, 0x85, 0x40, 0xb7, 0x2b,
				0x75, 0x38, 0x4a, 0x3e, 0x25, 0x0a, 0xb6, 0x58,
				0xe3, 0x9a, 0x7c, 0xde, 0xd5,
			],
			b = [
				0x04, 0x01, 0x01, 0xbf, 0xd2, 0xa7, 0xed, 0xe7,
				0x68, 0x5a, 0x06, 0x4b, 0xc8, 0x40, 0x42, 0x9d,
				0xea, 0x5c, 0x05, 0x3e, 0x3d, 0x3d, 0x48, 0xc6,
				0x38, 0xb0, 0x0f, 0xd4, 0x35, 0x67, 0x67, 0xb2,
				0x12, 0xa7, 0x0a, 0xd4, 0xc3, 0x22, 0x3f, 0x7d,
				0xff, 0xe2, 0x03, 0x8e, 0x72, 0x71, 0x2c, 0x24,
				0xd5, 0x91, 0x0c, 0x9c, 0xb2, 0xc7, 0x2d, 0x5c,
				0xe1, 0xf7, 0x07, 0x49, 0x09, 0xeb, 0xe4, 0x26,
				0xab, 0xe6, 0x0d, 0x01, 0x26, 0xb1, 0x8e, 0x19,
				0xb3, 0xe3, 0x72, 0xe9, 0xf4, 0x15, 0xe3, 0x52,
				0x2a, 0xb3, 0xcb, 0xac, 0xf8, 0xe7, 0xc0, 0x14,
				0x60, 0x97, 0x71, 0xa0, 0x54, 0x2b, 0x94, 0x4c,
				0x13, 0x6e, 0xfb, 0x98, 0x11, 0x72, 0x60, 0x6a,
				0x0e, 0xd5, 0xb4, 0xe8, 0x17, 0x6c, 0x09, 0x2a,
				0xff, 0x89, 0xac, 0x88, 0xcd, 0x56, 0xe4, 0xcc,
				0x66, 0x79, 0x8c, 0xb7, 0xd2, 0x44, 0x44, 0x60,
				0xd8, 0x04, 0xa2, 0x50, 0x0f,
			],
			x = [
				0x01, 0x74, 0xe2, 0x0e, 0x0c, 0xa6, 0xd0, 0x12,
				0xb0, 0xc3, 0x86, 0xbc, 0xfc, 0x9a, 0xcb, 0x09,
				0x7a, 0xf9, 0xca, 0xb7, 0xc8, 0x79, 0x39, 0x3e,
				0xb6, 0x8e, 0x3e, 0x2f, 0x02, 0x6a, 0xfd, 0x07,
				0x65, 0xe9, 0x97, 0xe1, 0xf5, 0xf0, 0x16, 0x9a,
				0xa2, 0xe6, 0x03, 0x75, 0x1f, 0xa2, 0xf4, 0xe5,
				0xcd, 0x54, 0x94, 0x60, 0xb2, 0xfd, 0xe7, 0x97,
				0xea, 0x72, 0x02, 0xb9, 0x96, 0x48, 0xd3, 0x45,
				0xc2, 0x26,
			],
			y = [
				0x01, 0xae, 0xa0, 0x8f, 0x5b, 0x13, 0xe1, 0x85,
				0x4f, 0xfe, 0xcf, 0x73, 0x6a, 0x18, 0xc3, 0xfa,
				0xb7, 0xe6, 0xfc, 0xe5, 0xa9, 0x09, 0x8a, 0x68,
				0x4f, 0x49, 0x93, 0x59, 0xeb, 0xfd, 0x91, 0xf9,
				0x45, 0x1d, 0xcf, 0x51, 0x61, 0x39, 0x5c, 0x87,
				0x6c, 0x70, 0x9d, 0xfa, 0x7a, 0x86, 0x30, 0x64,
				0x3a, 0x4f, 0x48, 0x78, 0x3a, 0x2f, 0x9f, 0x84,
				0x07, 0xc1, 0x94, 0x5a, 0xc7, 0x1a, 0xe2, 0x5d,
				0x73, 0xb3,
			],
			result = 0,
			expected = [],
		},
	];

	tmuladd(p521, tcs);
};
